﻿using System;
using System.Reflection;
using System.IO;
using System.Net;
using System.Xml;  
  
namespace UpdateHelper
{
    public class SoftUpdate
    {
        private string download;
        private const string updateUrl = "http://yy.tianlai.net.cn/UpdateConf.xml";
 
        #region 构造函数  
        public SoftUpdate() { }

        /// <param name="file">要更新的文件</param>  
        public SoftUpdate(string checkFile, string softName, string downFile)
        {
            this.LoadFile = checkFile;
            this.SoftName = softName;
            this.downFileName = downFile;
        }
        #endregion

        #region 属性  
        private string loadFile;
        private string newVerson;
        private string softName;
        private bool isUpdate;

        /// <summary>  
        /// 或取是否需要更新  
        /// </summary>  
        public bool IsUpdate
        {
            get
            {
                checkUpdate();
                return isUpdate;
            }
        }

        /// <summary>  
        /// 要检查更新的文件  
        /// </summary>  
        public string LoadFile
        {
            get { return loadFile; }
            set { loadFile = value; }
        }

        /// <summary>  
        /// 程序集新版本  
        /// </summary>  
        public string NewVerson
        {
            get { return newVerson; }
        }

        /// <summary>  
        /// 升级的名称  
        /// </summary>  
        public string SoftName
        {
            get { return softName; }
            set { softName = value; }
        }

        public string downFileName { get; set; }
        public string whatNew { get; private set; }
        public Version oldVer { get; private set; }
        public Version newVer { get; private set; }
        #endregion

        /// <summary>  
        /// 更新完成时触发的事件  
        /// </summary>  
        public event Action UpdateFinish;
        private void isFinish()
        {
            if (UpdateFinish != null)
                UpdateFinish();
        }

        /// <summary>  
        /// 下载更新  
        /// </summary>  
        public void Update()
        {
            //try
            //{
                if (!isUpdate)
                    return;
                WebClient wc = new WebClient();
                wc.DownloadFile(download, downFileName);
                wc.Dispose();
                isFinish();
            //}
            //catch
            //{
                //throw new Exception("更新出现错误，网络连接失败！");
            //}
        }

        /// <summary>  
        /// 检查是否需要更新  
        /// </summary>  
        public void checkUpdate()
        {
            try
            {
                WebClient wc = new WebClient();
                Stream stream = wc.OpenRead(updateUrl);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(stream);
                XmlNode list = xmlDoc.SelectSingleNode("Update");
                foreach (XmlNode node in list)
                {
                    if (node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == SoftName.ToLower())
                    {
                        foreach (XmlNode xml in node)
                        {
                            switch (xml.Name)
                            {
                                case "Verson":
                                    newVerson = xml.InnerText;
                                    break;
                                case "DownLoad":
                                    download = xml.InnerText;
                                    break;
                                case "WhatNew":
                                    whatNew = xml.InnerText;
                                    break;
                            }
                        }
                    }
                }

                newVer = new Version(newVerson);
                oldVer = Assembly.LoadFrom(loadFile).GetName().Version;
                if (newVer.Major == oldVer.Major && newVer.Minor == oldVer.Minor && newVer.Build == oldVer.Build)
                    isUpdate = false;
                else
                    isUpdate = true;
            }
            catch (Exception ex)
            {
                throw new Exception("更新出现错误，请确认网络连接无误后重试！");
            }
        }

        /// <summary>  
        /// 获取要更新的文件  
        /// </summary>  
        /// <returns></returns>  
        public override string ToString()
        {
            return this.loadFile;
        }
    }
}